<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<% String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":"
            + request.getServerPort() + path + "/"; %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <base href="<%= basePath %>"/>
    <title>销售订单管理</title>
    <%-- <link rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">--%>
    <%-- <link rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.0/font/bootstrap-icons.css">--%>
    <link rel="stylesheet" href="/app/pages/css/bootstrap.min.css">
    <link rel="stylesheet" href="/app/pages/css/bootstrap-icons.css">
    <style>
        .table th,
        .table td {
            vertical-align: middle;
        }

        .order-status-0 {
            color: #ffc107;
        }

        .order-status-1 {
            color: #28a745;
        }

        .payment-status-0 {
            color: #dc3545;
        }

        .payment-status-1 {
            color: #28a745;
        }
    </style>
</head>

<body>
<div class="container-fluid">
    <div class="row">


        <!-- 主内容区 -->
        <div class="col-md-10 p-4">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2>入库订单管理</h2>
                <div>
<%--                    <button class="btn btn-primary" data-toggle="modal"--%>
<%--                            data-target="#addOrderModal">--%>
<%--                        <i class="bi bi-plus-circle"></i> 新增入库订单--%>
<%--                    </button>--%>
                    <%--                    <button class="btn btn-outline-secondary ml-2">--%>
                    <%--                        <i class="bi bi-file-earmark-excel"></i> 导出Excel--%>
                    <%--                    </button>--%>
                </div>
            </div>

            <!-- 搜索筛选区 -->
            <div class="card mb-4">
                <div class="card-body">
                    <form id="searchForm" class="row">
<%--                        <div class="form-group col-md-3">--%>
<%--                            <label for="orderCode">订单编号</label>--%>
<%--                            <input type="text" class="form-control" id="orderCode" name="code">--%>
<%--                        </div>--%>
                        <div class="form-group col-md-3">
                            <label for="userId">供应商名字</label>
                            <input type="text" class="form-control" id="userId" name="name">
                        </div>
<%--                        <div class="form-group col-md-3">--%>
<%--                            <label for="paymentStatus">支付状态</label>--%>
<%--                            <select class="form-control" id="paymentStatus"--%>
<%--                                    name="paymentStatus">--%>
<%--                                <option value="">全部</option>--%>
<%--                                <option value="0">未支付</option>--%>
<%--                                <option value="1">已支付</option>--%>
<%--                            </select>--%>
<%--                        </div>--%>
                        <div class="form-group col-md-3">
                            <label for="orderStatus">订单状态</label>
                            <select class="form-control" id="orderStatus" name="orderStatus">
                                <option value="">全部</option>
                                <option value="0">待发货</option>
                                <option value="1">已发货</option>
                            </select>
                        </div>
                        <div class="form-group col-md-4">
                            <label>订单时间范围</label>
                            <div class="d-flex">
                                <input type="date" class="form-control" name="startDate">
                                <span class="mx-2 align-self-center">至</span>
                                <input type="date" class="form-control" name="endDate">
                            </div>
                        </div>
                        <div class="form-group col-md-2 align-self-end">
                            <button type="submit" class="btn btn-primary btn-block">
                                <i class="bi bi-search"></i> 搜索
                            </button>
                        </div>
                        <div class="form-group col-md-2 align-self-end">
                            <button type="reset" class="btn btn-outline-secondary btn-block">
                                <i class="bi bi-arrow-repeat"></i> 重置
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            <!-- 订单表格 -->
            <div class="card">
                <div class="card-body p-0">
                    <table class="table table-hover table-striped mb-0">
                        <thead class="thead-light">
                        <tr>
                            <th>ID</th>
                            <th>订单编号</th>
                            <th>供应商ID</th>
                            <th>供应商名称</th>
                            <th>订单总金额</th>
                            <th>支付状态</th>
                            <th>订单状态</th>
                            <th>备注</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="orderTableBody">
                        <c:forEach items="${list}" var="order">
                            <tr>
                                <td>${order.id}</td>
                                <td>${order.code}</td>
                                <td>${order.supplierid}</td>
                                <td>${order.suname}</td>
                                <td>¥${order.totalamount}</td>
                                <td><span
                                        class="badge badge-pill payment-status-1">已结清</span></td>
                                <td><span
                                        class="badge badge-pill ${order.orderstatus == 0 ? 'order-status-0' : 'order-status-1'}">${order.orderstatus
                                        == 0 ? '待发货' : '已发货'}</span></td>
                                <td>${order.remark}</td>
                                <td>${order.ordertime}</td>
                                <td>
                                    <div class="btn-group btn-group-sm">
                                        <button class="btn btn-info btn-view-order"
                                                data-toggle="tooltip" title="查看详情"
                                                data-id="${order.id}" data-code="${order.code}"
                                                data-userid="${order.supplierid}"
                                                data-ordertime="${order.ordertime}"
                                                data-totalamount="${order.totalamount}"
                                                data-paymentstatus="1"
                                                data-orderstatus="${order.orderstatus}"
                                                data-remark="${order.remark}">
                                            <i class="bi bi-eye"></i>
                                        </button>
                                        <button class="btn btn-primary btn-edit-order"
                                                data-toggle="tooltip" title="编辑"
                                                data-id="${order.id}" data-code="${order.code}"
                                                data-userid="${order.supplierid}"
                                                data-ordertime="${order.ordertime}"
                                                data-totalamount="${order.totalamount}"
                                                data-paymentstatus="1"
                                                data-orderstatus="${order.orderstatus}"
                                                data-remark="${order.remark}">
                                            <i class="bi bi-pencil"></i>
                                        </button>
                                        <button class="btn btn-danger btn-delete-order"
                                                data-toggle="tooltip" title="删除"
                                                data-id="${order.id}" data-code="${order.code}">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
                <div class="card-footer">
                    <nav aria-label="Page navigation">
                        <ul class="pagination justify-content-end mb-0">
                            <li class="page-item ${param.page <= 1 ? 'disabled' : ''}">
                                <a class="page-link"
                                   href="purchase_order?page=${param.page-1 < 1 ? 1 : param.page-1}"
                                   tabindex="-1">上一页</a>
                            </li>

                            <!-- 分页逻辑：显示首页、尾页和当前页附近的页码 -->
                            <c:set var="currentPage"
                                   value="${empty param.page ? 1 : param.page}"/>
                            <c:set var="totalPages" value="${count}"/>
                            <c:set var="displayPages" value="5"/> <!-- 当前页附近显示的页数 -->

                            <!-- 计算开始和结束页码 -->
                            <c:set var="halfDisplayPages" value="${displayPages / 2}"/>
                            <c:set var="beginPage" value="${currentPage - halfDisplayPages}"/>
                            <c:if test="${beginPage < 2}">
                                <c:set var="beginPage" value="2"/>
                            </c:if>

                            <c:set var="endPage" value="${currentPage + halfDisplayPages}"/>
                            <c:if test="${endPage >= totalPages}">
                                <c:set var="endPage" value="${totalPages - 1}"/>
                            </c:if>

                            <!-- 显示第一页 -->
                            <li class="page-item ${currentPage == 1 ? 'active' : ''}">
                                <a class="page-link" href="purchase_order?page=1">1</a>
                            </li>

                            <!-- 如果当前页大于displayPages+2，显示前省略号 -->
                            <c:if test="${beginPage > 2}">
                                <li class="page-item disabled">
                                    <a class="page-link" >...</a>
                                </li>
                            </c:if>

                            <!-- 显示当前页附近的页码 -->
                            <c:forEach begin="${beginPage}" end="${endPage}" var="pages">
                                <c:if test="${pages > 1 && pages < totalPages}">
                                    <li
                                            class="page-item ${pages == currentPage ? 'active' : ''}">
                                        <a class="page-link"
                                           href="purchase_order?page=${pages}">${pages}</a>
                                    </li>
                                </c:if>
                            </c:forEach>

                            <!-- 如果当前页小于总页数-displayPages-1，显示后省略号 -->
                            <c:if test="${endPage < totalPages - 1}">
                                <li class="page-item disabled">
                                    <a class="page-link" >...</a>
                                </li>
                            </c:if>

                            <!-- 显示最后一页，如果总页数大于1 -->
                            <c:if test="${totalPages > 1}">
                                <li
                                        class="page-item ${currentPage == totalPages ? 'active' : ''}">
                                    <a class="page-link"
                                       href="purchase_order?page=${totalPages}">${totalPages}</a>
                                </li>
                            </c:if>

                            <li class="page-item ${param.page >= totalPages ? 'disabled' : ''}">
                                <a class="page-link"
                                   href="purchase_order?page=${param.page+1 > totalPages ? totalPages : param.page+1}">下一页</a>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 新增订单模态框 -->
<div class="modal fade" style="margin-top: 100px" id="addOrderModal" tabindex="-1" aria-labelledby="addOrderModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addOrderModalLabel">新增销售订单</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="addOrderForm">
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="newOrderCode">订单编号</label>
                            <input type="text" class="form-control" id="newOrderCode" name="code"
                                   required placeholder="不填写系统会自动生成">
                        </div>
                        <div class="form-group col-md-6">
                            <label for="newUserId">供应商ID</label>
                            <input type="number" class="form-control" id="newUserId" name="userId"
                                   required>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="newTotalAmount">订单总金额</label>
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">¥</span>
                                </div>
                                <input type="number" step="0.01" class="form-control"
                                       id="newTotalAmount" name="totalAmount" required>
                            </div>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="newOrderTime">订单时间</label>
                            <input type="datetime-local" class="form-control" id="newOrderTime"
                                   name="orderTime" required>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="newPaymentStatus">支付状态</label>
                            <select class="form-control" id="newPaymentStatus" name="paymentStatus"
                                    required>
                                <option value="0">未支付</option>
                                <option value="1">已支付</option>
                            </select>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="newOrderStatus">订单状态</label>
                            <select class="form-control" id="newOrderStatus" name="orderStatus"
                                    required>
                                <option value="0">待发货</option>
                                <option value="1">已发货</option>
                            </select>
                        </div>
                        <div class="form-group col-12">
                            <label for="newRemark">备注</label>
                            <textarea class="form-control" id="newRemark" name="remark"
                                      rows="3"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="saveOrderBtn">保存</button>
            </div>
        </div>
    </div>
</div>

<!-- 编辑订单模态框 -->
<div class="modal fade" style="margin-top: 100px" id="editOrderModal" tabindex="-1"
     aria-labelledby="editOrderModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editOrderModalLabel">编辑销售订单</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form id="editOrderForm">
                    <input type="hidden" id="editOrderId" name="id">
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label for="editOrderCode">订单编号</label>
                            <input type="text" class="form-control" id="editOrderCode" name="code"
                                   required disabled>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="editUserId">供应商ID</label>
                            <input type="number" class="form-control" id="editUserId" name="userId"
                                   required disabled>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="editTotalAmount">订单总金额</label>
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">¥</span>
                                </div>
                                <input type="number" step="0.01" class="form-control"
                                       id="editTotalAmount" name="totalAmount" required disabled>
                            </div>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="editOrderTime">订单时间</label>
                            <input type="datetime-local" class="form-control" id="editOrderTime"
                                   name="orderTime" required disabled>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="editPaymentStatus">支付状态</label>
                            <select class="form-control" id="editPaymentStatus" name="paymentStatus"
                                    required disabled>
                                <option value="0">未支付</option>
                                <option value="1">已支付</option>
                            </select>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="editOrderStatus">订单状态</label>
                            <select class="form-control" id="editOrderStatus" name="orderStatus"
                                    required disabled>
                                <option value="0">待发货</option>
                                <option value="1">已发货</option>
                            </select>
                        </div>
                        <div class="form-group col-12">
                            <label for="editRemark">备注</label>
                            <textarea class="form-control" id="editRemark" name="remark"
                                      rows="3"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="updateOrderBtn">保存修改</button>
            </div>
        </div>
    </div>
</div>

<!-- 查看订单详情模态框 -->
<div class="modal fade" style="margin-top: 100px" id="viewOrderModal" tabindex="-1"
     aria-labelledby="viewOrderModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="viewOrderModalLabel">订单详情</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-6">
                        <p><strong>订单ID:</strong> <span id="viewOrderId"></span></p>
                        <p><strong>订单编号:</strong> <span id="viewOrderCode"></span></p>
                        <p><strong>供应商ID:</strong> <span id="viewUserId"></span></p>
                        <p><strong>订单时间:</strong> <span id="viewOrderTime"></span></p>
                    </div>
                    <div class="col-md-6">
                        <p><strong>订单总金额:</strong> <span id="viewTotalAmount"></span></p>
                        <p><strong>支付状态:</strong> <span id="viewPaymentStatus"></span></p>
                        <p><strong>订单状态:</strong> <span id="viewOrderStatus"></span></p>
                        <p><strong>创建时间:</strong> <span id="viewCreateTime"></span></p>
                    </div>
                    <div class="col-12">
                        <p><strong>备注:</strong> <span id="viewRemark"></span></p>
                    </div>
                </div>
                <hr>
                <h6>订单详情</h6>
                <table class="table table-sm table-bordered">
                    <thead>
                    <tr>
                        <th>商品名称</th>
                        <th>茶产品等级</th>
                        <th>单价</th>
                        <th>数量</th>
                        <th>小计</th>
                    </tr>
                    </thead>
                    <tbody id="viewOrderItems">
                    <!-- 订单详情项将通过JavaScript动态添加 -->
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- 确认删除模态框 -->
<div class="modal fade" id="deleteOrderModal" style="margin-top: 260px" tabindex="-1" aria-labelledby="deleteOrderModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteOrderModalLabel">确认删除</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>您确定要删除订单 <strong id="deleteOrderCode"></strong> 吗？此操作不可逆。</p>
                <input type="hidden" id="deleteOrderId">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" id="confirmDeleteBtn">确认删除</button>
            </div>
        </div>
    </div>
</div>
<%--<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>--%>
<%--<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js">
    </script>--%>
<script src="/app/pages/js/jquery.min.js"></script>
<script src="/app/pages/js/bootstrap.bundle.min.js"></script>
<script>
    $(function () {
        // 初始化工具提示
        $('[data-toggle="tooltip"]').tooltip();

        // 保存订单按钮点击事件
        $('#saveOrderBtn').click(function () {
            // 这里添加表单验证和提交逻辑
            alert('订单保存功能需要后端实现');
        });

        // 搜索表单提交事件
        $('#searchForm').submit(function (e) {
            e.preventDefault();
            //输出表单 数据
            // console.log($(this).serialize());
            location.href='/app/ps?'+$(this).serialize();
            // alert('搜索功能需要后端实现');
        });

        // 编辑按钮点击事件
        $(document).on('click', '.btn-edit-order', function () {
            var orderId = $(this).data('id');
            var orderCode = $(this).data('code');
            var userId = $(this).data('userid');
            var orderTime = $(this).data('ordertime');
            var totalAmount = $(this).data('totalamount');
            var paymentStatus = $(this).data('paymentstatus');
            var orderStatus = $(this).data('orderstatus');
            var remark = $(this).data('remark');

            var formattedTime = formatDateTimeForInput(orderTime);

            // 填充表单
            $('#editOrderId').val(orderId);
            $('#editOrderCode').val(orderCode);
            $('#editUserId').val(userId);
            $('#editOrderTime').val(formattedTime).prop('disabled', true);
            $('#editTotalAmount').val(totalAmount);
            $('#editPaymentStatus').val(paymentStatus);
            $('#editOrderStatus').val(orderStatus);
            $('#editRemark').val(remark);

            // 显示模态框
            $('#editOrderModal').modal('show');
        });

        // 更新订单按钮点击事件
        $('#updateOrderBtn').click(function () {
            // 收集表单数据
            var formData = {
                id: $('#editOrderId').val(),
                // paymentstatus: $('#editPaymentStatus').val(),
                // orderstatus: $('#editOrderStatus').val(),
                remark: $('#editRemark').val()
            };

            //用ajax提交数据
            $.ajax({
                type: 'POST',
                url: '/app/purchang',
                data: formData,
                dataType: 'json',
                success: function (result) {
                    console.log("服务器响应:", result);
                    if (result.code == 200) {
                        //关闭编辑框
                        $('#editOrderModal').modal('hide');
                        setTimeout(function () {
                            location.reload();
                        }, 100);
                    } else {
                        alert("订单更新失败: " + (result.data || "未知错误"));
                    }
                },
                error: function (xhr, status, error) {
                    console.error("Ajax错误:", error);
                    console.error("状态:", status);
                    console.error("响应:", xhr.responseText);
                    alert("请求发生错误，请查看控制台");
                }
            });

            // 在控制台输出表单信息
            console.log("表单数据:", formData);
        });

        // 查看详情按钮点击事件
        $(document).on('click', '.btn-view-order', function () {
            var orderId = $(this).data('id');
            var orderCode = $(this).data('code');
            var userId = $(this).data('userid');
            var orderTime = $(this).data('ordertime');
            var totalAmount = $(this).data('totalamount');
            var paymentStatus = $(this).data('paymentstatus') == 0 ? '未支付' : '已支付';
            var orderStatus = $(this).data('orderstatus') == 0 ? '待发货' : '已发货';
            var remark = $(this).data('remark');

            // 填充详情
            $('#viewOrderId').text(orderId);
            $('#viewOrderCode').text(orderCode);
            $('#viewUserId').text(userId);
            $('#viewOrderTime').text(orderTime);
            $('#viewTotalAmount').text('¥' + totalAmount);
            $('#viewPaymentStatus').text(paymentStatus);
            $('#viewOrderStatus').text(orderStatus);
            $('#viewCreateTime').text(orderTime); // 使用orderTime代替createTime
            $('#viewRemark').text(remark || '无');

            $.ajax({
                method: 'POST',
                url: '/app/purchase_order_detail',
                data: {
                    orderid: orderId
                },
                dataType: 'json',
                success: function (data) {
                    if (data.code === 200) {
                        let arr = "";
                        data.data.forEach(function (item) {
                            arr += `
                            <tr>
                                <td>` + item.proname + `</td>
                                <td>` + item.level + `</td>
                                <td>¥` + item.unitprice + `</td>
                                <td>` + item.proquantity + `</td>
                                <td>¥` + item.unitprice * item.proquantity + `</td>
                            </tr>
                            `
                        })
                        $('#viewOrderItems').html(arr);
                    } else {

                    }
                }
            })

            // 显示模态框
            $('#viewOrderModal').modal('show');
        });

        // 删除按钮点击事件
        $(document).on('click', '.btn-delete-order', function () {
            var orderId = $(this).data('id');
            var orderCode = $(this).data('code');

            $('#deleteOrderId').val(orderId);
            $('#deleteOrderCode').text(orderCode);

            $('#deleteOrderModal').modal('show');
        });

        // 确认删除按钮点击事件
        $('#confirmDeleteBtn').click(function () {
            var orderId = $('#deleteOrderId').val();
            $.ajax({
                url: '/app/purdelete',
                type: 'POST',
                data: {
                    id: orderId
                },
                dataType: 'json',
                success: function (data) {
                    if (data.code == 200) {
                        window.location.reload();
                    } else {
                        alert(data.data)
                    }
                }
            })

            // // 这里添加删除逻辑
            // alert('订单删除功能需要后端实现');

            $('#deleteOrderModal').modal('hide');
        });

        // 辅助函数：将日期时间格式化为input datetime-local可用的格式
        function formatDateTimeForInput(dateTimeStr) {
            if (!dateTimeStr) return '';

            // 尝试直接解析常见格式
            var formattedDate = '';

            // 检查是否是 yyyy-MM-dd HH:mm:ss 格式
            if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(dateTimeStr)) {
                return dateTimeStr.replace(' ', 'T').substring(0, 16);
            }

            // 检查是否是 yyyy/MM/dd HH:mm:ss 格式
            if (/^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}$/.test(dateTimeStr)) {
                return dateTimeStr.replace('/', '-').replace('/', '-').replace(' ', 'T').substring(0, 16);
            }

            // 尝试使用Date对象解析
            try {
                var date = new Date(dateTimeStr);
                if (!isNaN(date.getTime())) {
                    // 格式化为 yyyy-MM-ddTHH:mm
                    var year = date.getFullYear();
                    var month = (date.getMonth() + 1).toString().padStart(2, '0');
                    var day = date.getDate().toString().padStart(2, '0');
                    var hours = date.getHours().toString().padStart(2, '0');
                    var minutes = date.getMinutes().toString().padStart(2, '0');

                    return `${year}-${month}-${day}T${hours}:${minutes}`;
                }
            } catch (e) {
                console.error("日期解析错误:", e);
            }

            // 如果上述方法都失败，尝试手动解析
            try {
                var parts = dateTimeStr.split(/[- :\/]/);
                if (parts.length >= 6) {
                    // 假设格式为 yyyy-MM-dd HH:mm:ss 或 yyyy/MM/dd HH:mm:ss
                    var year = parseInt(parts[0]);
                    var month = parseInt(parts[1]).toString().padStart(2, '0');
                    var day = parseInt(parts[2]).toString().padStart(2, '0');
                    var hours = parseInt(parts[3]).toString().padStart(2, '0');
                    var minutes = parseInt(parts[4]).toString().padStart(2, '0');

                    return `${year}-${month}-${day}T${hours}:${minutes}`;
                }
            } catch (e) {
                console.error("手动日期解析错误:", e);
            }

            // 如果仍然无法解析，返回空字符串
            console.warn("无法解析日期:", dateTimeStr);
            return '';
        }
    });
</script>
</body>

</html>